package com.water.camera.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.water.camera.adapter.EditListAdapter;
import com.water.camera.bean.EditItem;
import com.water.camera.databinding.ActivityEditWaterMaskBinding;
import com.water.camera.fragment.BaseDialogFragment;
import com.water.camera.fragment.EditDlg;
import com.water.camera.fragment.EditPointDlg;
import com.water.camera.fragment.EditTimeDlg;
import com.water.camera.it.EditListener;
import com.water.camera.mark.BaseMark;

public class EditMarkActivity extends BaseActivity implements EditListener {

    ActivityEditWaterMaskBinding binding;
    BaseMark mark;
    EditListAdapter adapter;
    boolean change = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityEditWaterMaskBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.ivBack.setOnClickListener(v -> finish());
        binding.btnComplete.setOnClickListener(v -> finish());
        int from = getIntent().getIntExtra("from", 0);
        if (from == 1) {
            binding.btnSize.setVisibility(View.GONE);
        }

        String name = getIntent().getStringExtra("name");
        try {
            mark = (BaseMark) Class.forName(name).getDeclaredConstructor(
                    Context.class,
                    AttributeSet.class
            ).newInstance(this, null);

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER_HORIZONTAL;
            binding.nsvPreview.addView(mark, params);

            if(mark.isFullWidth() || mark.isFullScreen()){

            }
        } catch (Throwable e) {
            e.printStackTrace();
            finish();
            return;
        }

        binding.rvWatermark.setLayoutManager(new LinearLayoutManager(this));
        adapter = new EditListAdapter(mark.getEditItems(), this);
        binding.rvWatermark.setAdapter(adapter);

        binding.btnSize.setOnClickListener(v -> {
            Intent i = new Intent(EditMarkActivity.this, EditSizeActivity.class);
            i.putExtra("name", name);
            startActivityForResult(i, 10);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10 && resultCode == RESULT_OK) {
            change = true;
        }
    }

    public void toEdit(EditItem item, int p) {
        if (item.isTime) {
            EditTimeDlg dlg = new EditTimeDlg();
            dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                @Override
                public void onDismiss(boolean isOk, Object value) {
                    if (isOk) {
                        item.updateTime((Long) value);
                        adapter.notifyItemChanged(p);
                        onValueChange(item);
                    }
                }
            });
            dlg.show(getSupportFragmentManager(), "");
            return;
        } else if (item.key.contains("jingwei")) {
            EditPointDlg dlg = new EditPointDlg().now(item.key);
            dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                @Override
                public void onDismiss(boolean isOk, Object value) {
                    if (isOk) {
                        item.updatePoint(value.toString());
                        adapter.notifyItemChanged(p);
                        onValueChange(item);
                    }
                }
            });
            dlg.show(getSupportFragmentManager(), "");
            return;
        }
        EditDlg dlg = new EditDlg().setData(item);
        dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
            @Override
            public void onDismiss(boolean isOk, Object value) {
                if (isOk && value instanceof EditItem) {
                    item.value = ((EditItem) value).value;
                    adapter.notifyItemChanged(p);
                    onValueChange(item);
                }
            }
        });
        dlg.show(getSupportFragmentManager(), "");
    }

    @Override
    public void onShowChange(EditItem item) {
        change = true;
        mark.onShowChange(item);
    }

    @Override
    public void onValueChange(EditItem item) {
        change = true;
        mark.onValueChange(item);
    }

    @Override
    public void finish() {
        if (change) {
            setResult(RESULT_OK);
        }
        super.finish();
    }
}
